/**
 * 初始化报考学校详情对话框
 */
var EnrollSchoolInfoDlg = {
    enrollSchoolInfoData : {},
    validateFields : {
    	schoolCode : {
			validators : {
				notEmpty : {
					message : '报考学校代号不能为空'
				},
				stringLength : {
					max : 50,
					message : '报考学校代号长度不能超过50'
				}
			}
		},
		schoolName : {
			validators : {
				notEmpty : {
					message : '报考学校名称不能为空'
				},
				stringLength : {
					max : 200,
					message : '报考学校名称长度不能超过200'
				}
			}
		},
		batchTypes : {
    		trigger:"change",
			validators : {
				notEmpty : {
					message : '参与报考批次不能为空'
				}
			}
		}
	}
};

/**
 * 清除数据
 */
EnrollSchoolInfoDlg.clearData = function() {
    this.enrollSchoolInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EnrollSchoolInfoDlg.set = function(key, val) {
    this.enrollSchoolInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EnrollSchoolInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
EnrollSchoolInfoDlg.close = function() {
    parent.layer.close(window.parent.EnrollSchool.layerIndex);
}

/**
 * 收集数据
 */
EnrollSchoolInfoDlg.collectData = function() {
    this
    .set('id')
    .set('schoolCode')
    .set('schoolName')
    .set('batchTypes',$("#batchTypes").select2("val").join(","))
    ;
}

/**
 * 提交添加
 */
EnrollSchoolInfoDlg.addSubmit = function() {
    if (!this.validate()) {
        return;
    }
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/enrollSchool/add", function(data){
        Feng.success("添加成功!");
        window.parent.EnrollSchool.table.refresh();
        EnrollSchoolInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.enrollSchoolInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
EnrollSchoolInfoDlg.editSubmit = function() {
    if (!this.validate()) {
        return;
    }
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/enrollSchool/update", function(data){
        Feng.success("修改成功!");
        window.parent.EnrollSchool.table.refresh();
        EnrollSchoolInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.enrollSchoolInfoData);
    ajax.start();
}


/**
 * 验证数据是否为空
 */
EnrollSchoolInfoDlg.validate = function() {
	$('#enrollSchoolForm').data("bootstrapValidator").resetForm();
	$('#enrollSchoolForm').bootstrapValidator('validate');
	return $("#enrollSchoolForm").data('bootstrapValidator').isValid();
};

$(function() {
	Feng.initValidator("enrollSchoolForm", EnrollSchoolInfoDlg.validateFields);
});
